package com.itheima.ydd.service.impl;

import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.ydd.common.BusinessException;
import com.itheima.ydd.common.ResultData;
import com.itheima.ydd.entity.YddAdvimg;
import com.itheima.ydd.entity.YddAdvpos;
import com.itheima.ydd.mapper.YddAdvposMapper;
import com.itheima.ydd.service.IYddAdvimgService;
import com.itheima.ydd.service.IYddAdvposService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author author
 * @since 2022-05-22
 */
@Service
public class YddAdvposServiceImpl extends ServiceImpl<YddAdvposMapper, YddAdvpos> implements IYddAdvposService {
@Autowired
private IYddAdvimgService yddAdvimgService;

    //分页查询
    @Override
    public ResultData selectByPage(String advposname, Integer page, Integer limit) {
        Page<YddAdvpos> page1 = new Page<>(page,limit);
        LambdaQueryWrapper<YddAdvpos> wrapper = new LambdaQueryWrapper<>();
//        LambdaQueryWrapper<YddAdvpos> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(!StringUtils.isEmpty(advposname),YddAdvpos::getAdvposname,advposname);

        Page<YddAdvpos> page2 = page(page1, wrapper);

        HashMap<Object, Object> map = new HashMap<>();
        map.put("count",page2.getCountId());
        map.put("totalPages",page2.getTotal());
        map.put("pageSize",page2.getSize());
        map.put("currentPage",page2.getCurrent());
        map.put("data",page2.getRecords());
        return ResultData.ok(null,map);
    }

    //新增
    @Override
    public void add(YddAdvpos yddAdvpos) {

        try {
            save(yddAdvpos);
        } catch (Exception e) {
            throw new BusinessException("新增失败");
        }
    }

    //修改
    @Override
    public void updateyddArt(YddAdvpos yddAdvpos) {
        try {
            updateById(yddAdvpos);
        } catch (Exception e) {
            throw new BusinessException("修改失败");
        }
    }

    @Override
    public YddAdvpos selectById(Integer id) {
        YddAdvpos byId = getById(id);
        return byId;
    }

    @Override
    public ResultData selectByselectByall() {
        List<YddAdvpos> list = list();
        return ResultData.ok(null,list);
    }

}
